/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 中邮的权限修改
 */
import React, { useState, useCallback, useEffect } from 'react';
import { Modal, Checkbox, Form, Row, Col } from 'antd';
import { connect } from 'dva';

const FormItem = Form.Item;

const privilegeMap = {
  post: [
    // {
    //   lable: '快宝同城',
    //   value: 1
    // },
    // {
    //   lable: '即时配送',
    //   value: 2
    // },
    {
      lable: '中邮驿路',
      value: 3,
    },
    {
      lable: '中邮驿站',
      value: 6,
    },
    {
      lable: '订单管理',
      value: 4,
    },
    // {
    //   lable: '微信定制',
    //   value: 5
    // },
    {
      lable: '资金账户',
      value: 8,
    },
    {
      lable: '基础数据',
      value: 7,
    },
    // {
    //   lable: '财务管理',
    //   value: 9
    // },
    {
      lable: '账号管理',
      value: 10,
    },
  ],
  yjy: [
    {
      lable: '驿加易共配',
      value: 3,
    },
    {
      lable: '基础数据',
      value: 7,
    },
    {
      lable: '资金账户',
      value: 8,
    },
    {
      lable: '账号管理',
      value: 10,
    },
  ],
};

const Roles = props => {
  const {
    dispatch,
    uid,
    children,
    form,
    getListAfterAdded,
    loading,
    privilege: _privilege,
    branchLevel = '0',
    roleKey,
  } = props;
  const { getFieldDecorator, validateFields, resetFields } = form;
  const [visible, setVisible] = useState(false);
  const [list, setList] = useState(privilegeMap[roleKey] || []);

  useEffect(
    () => {
      if (branchLevel == 4) {
        setList(prevList => prevList.filter(i => i.value != 3));
      }
      if (roleKey == 'yjy' && branchLevel != 3) {
        setList(prevList => prevList.filter(i => i.value != 7));
      }
    },
    [branchLevel, roleKey],
  );

  const handleOk = useCallback(
    () => {
      validateFields((err, values) => {
        if (err) return;
        const { privilege } = values;
        dispatch({
          type: 'accounts/zyPrivilegeSet',
          payload: {
            uid,
            privilege: privilege.map(i => i).join(','),
          },
        }).then(() => {
          resetFields();
          getListAfterAdded();
          setVisible(false);
        });
      });
    },
    [uid, dispatch, getListAfterAdded, resetFields, validateFields],
  );

  const handleCancel = useCallback(
    () => {
      resetFields();
      setVisible(false);
    },
    [resetFields],
  );

  return (
    <>
      <span onClick={() => setVisible(true)}>{children}</span>
      <Modal
        width={585}
        centered
        title="权限分配"
        visible={visible}
        onCancel={handleCancel}
        onOk={handleOk}
        confirmLoading={loading}
      >
        <div style={{ marginBottom: 24 }}>为该账号分配可访问的功能菜单</div>
        <Form hideRequiredMark>
          <FormItem>
            {getFieldDecorator('privilege', {
              initialValue: _privilege,
            })(
              <Checkbox.Group>
                <Row type="flex" justify="start" gutter={[24, 16]}>
                  {list.map(item => (
                    <Col key={item.value}>
                      <Checkbox disabled={loading} value={item.value}>
                        {item.lable}
                      </Checkbox>
                    </Col>
                  ))}
                </Row>
              </Checkbox.Group>,
            )}
          </FormItem>
        </Form>
      </Modal>
    </>
  );
};

export default connect(({ loading, user, setting }) => ({
  currentUser: user.currentUser,
  loading: loading.effects['accounts/zyPrivilegeSet'],
  roleKey: setting.options.key,
}))(Form.create()(Roles));
